from app.admin import adminbp
from flask import render_template,request,jsonify,make_response
from app.models import BusinessSteps,Business,AlchemyEncoder
from app import db
from flask_login import login_required
import json

@adminbp.route('/')
@adminbp.route('/index', methods=['GET','POST'])
@login_required
def index():
    if request.method == 'POST':
        rsp = make_response()
        result= {'code':0,'message':'查询成功'}
        result['count']= db.session.query(Business).count()
        result['data'] = db.session.query(Business).order_by(Business.id.desc()).offset(request.values.get('page', 1, type=int)-1).limit(request.values.get('limit', 10, type=int)).all()
        rsp.data = json.dumps(result, cls=AlchemyEncoder)
        rsp.mimetype = 'application/json'
        return rsp
    return render_template('admin/index.html')

@adminbp.route('/business_steps', methods=['GET','POST'])
@login_required
def business_steps():
    if request.method == 'POST':
        rsp = make_response()
        result= {'code':0,'message':'查询成功'}
        result['count']= db.session.query(BusinessSteps).count()
        result['data'] = db.session.query(BusinessSteps).order_by(BusinessSteps.id.desc()).offset(request.values.get('page', 1, type=int)-1).limit(request.values.get('limit', 10, type=int)).all()
        rsp.data = json.dumps(result, cls=AlchemyEncoder)
        rsp.mimetype = 'application/json'
        return rsp
    return render_template('admin/business_steps.html')

@adminbp.route('/business_add', methods=['GET','POST'])
@login_required
def business_add():
    if request.method == 'POST':
        rsp = make_response()
        business = Business()
        business.business_code=request.values.get('business_code')
        business.business_name=request.values.get('business_name')
        business.business_desc=request.values.get('business_desc')
        business.business_extra=request.values.get('business_extra')
        business.business_is_extra=request.values.get('business_extra',0,type=int)
        db.session.add(business)
        db.session.commit()
        rsp.data = json.dumps({'code':1,'message':'添加成功'}, cls=AlchemyEncoder)
        rsp.mimetype = 'application/json'
        return rsp

    return render_template('admin/business_add.html')

@adminbp.route('/business_del', methods=['GET','POST'])
@login_required
def business_del():
    rsp = make_response()
    business =  Business.query.filter(Business.id==request.values.get('id')).first()
    db.session.delete(business)
    db.session.commit()
    rsp.data = json.dumps({'code':1,'message':'删除成功'}, cls=AlchemyEncoder)
    rsp.mimetype = 'application/json'
    return rsp


@adminbp.route('/business_step_add', methods=['GET','POST'])
@login_required
def business_step_add():
    if request.method == 'POST':
        rsp = make_response()
        business = BusinessSteps()
        business.business=request.values.get('business')
        business.name=request.values.get('name')
        business.type=request.values.get('type')
        business.var1=request.values.get('var1')
        business.var2=request.values.get('var2')
        business.var3=request.values.get('var3')
        business.var4=request.values.get('var4')
        business.pre=request.values.get('pre')
        business.content=request.values.get('content')
        business.post=request.values.get('post')
        business.sort=request.values.get('sort')
        db.session.add(business)
        db.session.commit()
        rsp.data = json.dumps({'code':1,'message':'添加成功'}, cls=AlchemyEncoder)
        rsp.mimetype = 'application/json'
        return rsp
    businesslist = db.session.query(Business).order_by(Business.id.desc()).all()
    return render_template('admin/business_step_add.html',businesslist=businesslist)
@adminbp.route('/business_step_edit', methods=['GET','POST'])
@login_required
def business_step_edit():
    if request.method == 'POST':
        rsp = make_response()
        business = BusinessSteps()
        business.business=request.values.get('business')
        business.name=request.values.get('name')
        business.type=request.values.get('type')
        business.var1=request.values.get('var1')
        business.var2=request.values.get('var2')
        business.var3=request.values.get('var3')
        business.var4=request.values.get('var4')
        business.pre=request.values.get('pre')
        business.content=request.values.get('content')
        business.post=request.values.get('post')
        business.sort=request.values.get('sort')
        db.session.add(business)
        db.session.commit()
        rsp.data = json.dumps({'code':1,'message':'添加成功'}, cls=AlchemyEncoder)
        rsp.mimetype = 'application/json'
        return rsp
    businesslist = db.session.query(Business).order_by(Business.id.desc()).all()
    step = db.session.query(BusinessSteps).filter(BusinessSteps.id==request.values.get('id')).first()
    return render_template('admin/business_step_edit.html',businesslist=businesslist,step = step)

@adminbp.route('/business_step_del', methods=['GET','POST'])
@login_required
def business_step_del():
    rsp = make_response()
    business =  BusinessSteps.query.filter(BusinessSteps.id==request.values.get('id')).first()
    db.session.delete(business)
    db.session.commit()
    rsp.data = json.dumps({'code':1,'message':'删除成功'}, cls=AlchemyEncoder)
    rsp.mimetype = 'application/json'
    return rsp

